package com.lq.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件写入
 *
 * @author liuqing
 * @since 2022/2/17
 */
public class FileOutputDemo {
    public static void main(String[] args) {
        File file = new File("src/liuqing.txt");
        FileOutputStream f1 = null;
        try {
            //想在原有的基础上追加内容,在创建写入流的时候设置一个append的参数为true
            f1 = new FileOutputStream(file,true);
            String str = "我是学生aa, i am student!!!";
            byte[] buff = str.getBytes(); // 把字符串转化为字节数组
            f1.write(buff);
            System.out.println("写入成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件未找到异常");
        } catch (Exception e) {
            System.out.println("写入异常");
        } finally {
            try {
                f1.close();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件关闭出现异常");
            }
        }
    }
}
